package com.zm.demo.core.concurrent;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2019/7/1 16:11
 */
public class CountDownLatchTest {


    public static void main(String[] args) throws Exception{
        //设置阻塞点，当完成初始值N的数量时，执行后面的逻辑。不可循环使用

        CountDownLatch countDownLatch = new CountDownLatch(2);
        ExecutorService pool = Executors.newFixedThreadPool(5);
        pool.submit(() -> {
            try {
                Thread.sleep(500L);
                out.println("煮饭...");
                countDownLatch.countDown();
            } catch (Exception e){
                e.printStackTrace();
            }
        });
        pool.submit(() -> {
            try {
                Thread.sleep(50000L);
                out.println("烧菜...");
                countDownLatch.countDown();
            } catch (Exception e){
                e.printStackTrace();
            }
        });
        countDownLatch.await();//等待
        out.println("吃饭...");
        pool.shutdown();
    }
}
